package com.jvm123.dytt

import com.google.gson.Gson
import groovy.sql.GroovyRowResult
import groovy.sql.Sql
import groovyx.net.http.ContentType
import groovyx.net.http.HTTPBuilder
import groovyx.net.http.Method

/**
 *
 * @author yawn
 * @date 2021/1/19 星期二
 *
 */

// sql db
url = "jdbc:mysql://localhost:3306/test?serverTimezone=UTC&characterEncoding=UTF-8&useUnicode=true"
sql = Sql.newInstance(url, "root", "root", "com.mysql.cj.jdbc.Driver")
List<GroovyRowResult> list = sql.rows("select (20000 + id) id, CONCAT('https://www.dytt.com', href) href, name, type, country, time, actors from movie_dytt limit 100")
int offset = 100
while (list != null && list.size() >= 1) {
    String listStr = new Gson().toJson(list)
    def mapList = convert(list)
    println(mapList)
    def http = new HTTPBuilder('http://petdy.cn:7700')
    http.post(path:'/indexes/movies/documents', body:mapList, requestContentType:ContentType.JSON){resp->
        println resp
    }
//    http.request(Method.POST) {
//        uri.path = '/indexes/movies/documents'
//        body = list
//        requestContentType = ContentType.JSON
//        response.success = { resp->
//            println resp
//        }
//    }
    offset += 100
    list = sql.rows("select (20000 + id) id, CONCAT('https://www.dytt.com', href) href, name, type, country, time, actors from movie_dytt limit ?, ?", [offset, 100])
    println offset
}



sql.close()

def convert(List<GroovyRowResult> list) {
//    List<Map> newList = new ArrayList<>()
//    for (GroovyRowResult row : list) {
//        def map = [id: row.get("id"), href: row.get("href").toString(), name: row.get("name").toString(), title: row.get("title").toString(), time: row.get("time").toString(), des: row.get("des").toString()]
//        newList.add(map)
//    }
//    return newList
    return list
}